#!/bin/sh
#
#     SUSE system statup script for Jenkins
#     Copyright (C) 2007  Pascal Bleser
#          
#     This library is free software; you can redistribute it and/or modify it
#     under the terms of the GNU Lesser General Public License as published by
#     the Free Software Foundation; either version 2.1 of the License, or (at
#     your option) any later version.
#			      
#     This library is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#      
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
#     USA.
#
### BEGIN INIT INFO
# Provides:          jenkins
# Required-Start:    $local_fs $remote_fs $network $named
# Should-Start: $time sendmail
# Required-Stop:     $local_fs $remote_fs $network $named
# Should-Stop: $time sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Jenkins continuous build server
# Description:       Start the Jenkins continuous build server
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
JENKINS_WAR="@@WAR@@"
test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
JENKINS_CONFIG=/etc/sysconfig/jenkins
test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

JENKINS_PID_FILE="/var/run/jenkins.pid"
JENKINS_USER="jenkins"
JENKINS_NICE="0"

# Read config	
. "$JENKINS_CONFIG"

. /etc/rc.status
rc_reset # Reset status of this service

# Set up environment accordingly to the configuration settings
[ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }
[ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 1; fi; }

if [ -z "$JENKINS_JAVA_HOME" ]; then
    . /etc/profile.d/alljava.sh
    [ -n "$JAVA_HOME" ] || { echo "Failed to determine JAVA_HOME, set JENKINS_JAVA_HOME in $JENKINS_CONFIG";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }
else
    JAVA_HOME="$JENKINS_JAVA_HOME"
fi
[ -d "$JAVA_HOME" ] || { echo "Invalid JENKINS_JAVA_HOME: directory does not exist: $JAVA_HOME";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }
[ -e "$JAVA_HOME/bin/java" ] || { echo "Invalid JENKINS_JAVA_HOME: bin/java not found under $JAVA_HOME";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }
export JAVA_HOME

JAVA_CMD="$JAVA_HOME/bin/java $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR"
PARAMS="--javaHome=$JAVA_HOME --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war"
[ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=$JENKINS_PORT"
[ -n "$JENKINS_AJP_PORT" ] && PARAMS="$PARAMS --ajp13Port=$JENKINS_AJP_PORT"
[ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL"
[ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP"
[ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX"
[ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE"
[ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS"

if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
    PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins/access_log"
fi

[ -z "$JENKINS_INIT_SHELL" -o -x "$JENKINS_INIT_SHELL" ] || { echo "JENKINS_INIT_SHELL does not refer to a shell: $JENKINS_INIT_SHELL";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

case "$1" in
    start)
	echo -n "Starting Jenkins "
        /sbin/checkproc -k -p "$JENKINS_PID_FILE" "$JAVA_HOME/bin/java" >/var/log/jenkins.rc 2>&1
        CHECK=$?
        if [ $CHECK -eq 7 ]; then
                rm -f "$JENKINS_PID_FILE"
                if [ -x "$JENKINS_INIT_SHELL" ]; then
                       startproc -n $JENKINS_NICE -s -e -l /var/log/jenkins.rc -p "$JENKINS_PID_FILE" -t 1 /bin/su -l -s "$JENKINS_INIT_SHELL" -c "$JAVA_CMD $PARAMS &" "$JENKINS_USER"
                else
                       HOME=$JENKINS_HOME startproc -n $JENKINS_NICE -s -e -l /var/log/jenkins.rc -u "$JENKINS_USER" -p "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS 
                fi
                JPROC=$( find /proc -maxdepth 2 -user $JENKINS_USER -name exe -lname "*/bin/java" )
                if [ -n "$JPROC" ]; then
                        basename `dirname $JPROC` >"$JENKINS_PID_FILE"
                        rc_status -v
                else
                        rc_failed
                        rc_status -v
                fi
        else
                rc_failed $CHECK
                rc_status -v
        fi
	;;
    stop)
	echo -n "Shutting down Jenkins "
	/sbin/killproc -p "$JENKINS_PID_FILE" "$JAVA_HOME/bin/java"
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service Jenkins "
	$0 try-restart
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service Jenkins "
	/sbin/checkproc -p "$JENKINS_PID_FILE" "$JAVA_HOME/bin/java"
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload, print out the
	## argument to this init script which is required for a reload.
	## Note: probe is not (yet) part of LSB (as of 1.9)

	test "$JENKINS_CONFIG" -nt "$JENKINS_PID_FILE" && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
